package cn.edu.neu.studentmsg_java.controller.workbench;

import cn.edu.neu.studentmsg_java.cons.R;
import cn.edu.neu.studentmsg_java.cons.State;
import cn.edu.neu.studentmsg_java.model.Classes;
import cn.edu.neu.studentmsg_java.model.Student;
import cn.edu.neu.studentmsg_java.service.ClassesService;
import cn.edu.neu.studentmsg_java.service.StudentService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

//学生控制层
@RestController
@RequestMapping("/workbench/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;

    //获取班级编号，并同时获取该班级的专业名称
    @RequestMapping("/getData")
    public R getData() {
        List<Object> list = new ArrayList<>();
        //获取班级及对应的专业名称
        List<Classes> MarjorList = classesService.findClassIdAndMajorName();
        //获取班级编号
        List<Integer> classList = classesService.findClassId();
        list.add(classList);
        list.add(MarjorList);
        if (ObjectUtils.isNotEmpty(list)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(list).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //根据任意条件进行学生查询
    @RequestMapping("/findStudentPart")
    public R findStudentPart(Student student) {
        //动态查询学生信息
        List<Student> studentList = studentService.findStudentPart(student);
        if (ObjectUtils.isNotEmpty(studentList)) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(studentList).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //通过学生编号删除学生信息
    @RequestMapping("/removeStudentById")
    public R removeStudentById(Integer student_id) {
        //通过student_id去删除学生信息
        int result = studentService.removeStudentById(student_id);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_DELETE_ERROR.getCode()).msg(State.DB_DELETE_ERROR.getMsg()).build();
    }

    //添加学生信息
    @RequestMapping("/addStudent")
    public R addStudent(@RequestBody Student student) {
        //获取当前年份并拼接“-9-1”得到当前年份的入学日期
        student.setEnter_date(Calendar.getInstance().get(Calendar.YEAR) + "-9-1");
        //添加学生工信息
        int result = studentService.addStudent(student);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_SAVE_ERROR.getCode()).msg(State.DB_SAVE_ERROR.getMsg()).build();
    }

    //通过student_id去查询学生信息
    @RequestMapping("/toModifyStudentPage")
    public R toModifyStudentPage(Integer student_id) {
        //通过student_id去查询学生信息
        Student student = studentService.findStudentByStudentId(student_id);
        if (student != null) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).data(student).build();
        }
        return R.builder().code(State.DB_QUERY_EMPTY.getCode()).msg(State.DB_QUERY_EMPTY.getMsg()).build();
    }

    //通过学生编号修改学生信息
    @RequestMapping("/modifyStudentById")
    public R modifyStudentById(@RequestBody Student student) {
        //通过学生编号修改学生信息
        int result = studentService.modifyStudentById(student);
        if (result > 0) {
            return R.builder().code(State.SUCCESS.getCode()).msg(State.SUCCESS.getMsg()).build();
        }
        return R.builder().code(State.DB_UPDATE_ERROR.getCode()).msg(State.DB_UPDATE_ERROR.getMsg()).build();
    }
}
